<?php

error_reporting(E_STRICT | E_ALL);
date_default_timezone_set('America/New_York');
ini_set('display_errors', 1);
set_time_limit(60*30);

$g_start = microtime(true);
require_once "config.php";
require_once dirname(__FILE__). '/util/connection.class.php';
$dbconn = Connection::getInstance();

$toSubUsersId = getToSubUsers($dbconn);
if(count($toSubUsersId)>0){
	$usersInfo = getUsersInfo($toSubUsersId, $dbconn);
	proffiliatesLiveSub($usersInfo, $dbconn);
//	setStatusSub($toSubUsersId, $dbconn);
}

echo "\n". (microtime(true)-$g_start) . " seconds\n";



function getToSubUsers(Connection $db)
{
	$sql = " SELECT user_id FROM proffiliateslive_subscriber WHERE status='tosub' limit 10";
	try{
		$result = $db->query($sql);
		if(!empty($result)){
			foreach($result as $k=>$v){
				$result[$k] = $v['user_id'];
			}	
		}
		return $result;
	}catch(Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

function getUsersInfo($usersId, Connection $db)
{
	$sql = " SELECT u.*, s.domain AS siteurl FROM users u LEFT JOIN sites s ON u.site_id=s.id WHERE u.id IN ( " . implode(', ', $usersId) . " ) ";
	try{
		$result = $db->query($sql);
		return $result;
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

function proffiliatesLiveSub($usersInfo, Connection $db)
{
	foreach($usersInfo as $user){		
		$params = array();
		$params['aid'] = 170;
		$params['email'] = trim($user['email']);
		$params['fn'] = trim($user['firstname']);
		$params['ln'] = trim($user['lastname']);
		$params['a1'] = $user['address1'];
		$params['a2'] = $user['address2'];
		$params['city'] = $user['city'];
		$params['st'] = $user['state'];
		$params['zip'] = $user['zipcode'];
		//$params['co'] = $user['country'];
		$params['subscribe'] = date('m-d-Y');
		$params['ph'] = $user['phone'];
		$params['ge'] = $user['gender'];
		$params['dob'] = date('m-d-Y', strtotime($user['birthday']));
		$params['src'] = strtolower(trim($user['siteurl']));
		$params['ip'] = long2ip($user['ip']);
		
		$ch = curl_init();
		$url = "https://lf.proffiliates.com/proffiliates/SubHandler?";
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, '', '&'));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		$content_xmlstr = curl_exec($ch);	var_dump($content_xmlstr);
		$content_obj = simplexml_load_string($content_xmlstr);
		print_r($content_obj);
		curl_close($ch);
		
		if($content_obj->resultcode == '0' && $content_obj->msg == 'success'){
			setStatusSub(array( $user['id'] ), 'sub', $db);
		}else{
			setStatusSub(array( $user['id'] ), 'sub_failed', $db);
		}
	}
}

function setStatusSub($usersId, $status, Connection $db)
{
	$sql = " UPDATE proffiliateslive_subscriber SET status=?, sub_date=? WHERE user_id IN ( " . implode(', ', $usersId) . " ) ";
	try{
		$result = $db->execute($sql, array( $status, date('Y-m-d') ));
		return $result;
	}catch (Exception $e){
		QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
		throw new Model_Exception($e->getMessage(), $e->getCode());
	}
}

	
	
	
	
	
	
	